const fs = require('fs');
const http = require('http');
// const https = require('https');
const {
	app,
	port,
	logger,
} = require('./server-express')
// require('./server-koa')


const server = http.createServer(app).listen(port);
// const server = https.createServer({
//   key: fs.readFileSync('./httpskey/server.key'),
//   cert: fs.readFileSync('./httpskey/server.crt')
// }, app).listen(port);

/**
 * Event listener for HTTP server "error" event.
 */
server.on('error', function onError(error) {
	if (error.syscall !== 'listen') {
		throw error;
		logger.error(error);
	}

	let bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;

	// handle specific listen errors with friendly messages
	switch (error.code) {
		case 'EACCES':
			logger.error(bind + ' requires elevated privileges');
			process.exit(1);
			break;
		case 'EADDRINUSE':
			logger.error(bind + ' is already in use');
			process.exit(1);
			break;
		default:
			throw error;
	}
});

/**
 * Event listener for HTTP server "listening" event.
 */
server.on('listening', function onListening() {
	let addr = server.address();
	let bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
	logger.info('Listening on ' + bind);
});